package com.xuecheng.teaching.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.Comment3Dto;
import com.xuecheng.commons.model.vo.CommentReplyVO;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.system.api.SystemApi;
import com.xuecheng.system.domain.User;
import com.xuecheng.teaching.domain.Comment;
import com.xuecheng.teaching.domain.CommentReply;
import com.xuecheng.teaching.mapper.CommentReplyMapper;
import com.xuecheng.teaching.service.CommentReplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.teaching.service.CommentService;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.BufferOverflowException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-08-13
 */
@Service
public class CommentReplyServiceImpl extends ServiceImpl<CommentReplyMapper, CommentReply> implements CommentReplyService {
    //  课程评论回复
    @Autowired
    private SystemApi systemApi;
    @Override
    public ResponseResult reply(Comment3Dto dto) {
        CommentReply commentReply = new CommentReply();
        // 评论内容
        commentReply.setReplyText(dto.getReplyText());
        // 外键字段
        commentReply.setCommentId(dto.getCommentId().longValue());
        // 获取用户名
        ResponseResult<User> uerInfo = systemApi.uerInfo(AuthInfoHolder.getUserId());
        String name = uerInfo.getData().getName();
        commentReply.setUserName(name);
        // 用户id
        commentReply.setUserId(AuthInfoHolder.getUserId());
       /* if (dto.getForbidReply()==1){
            throw new BusinessException(ErrorCode.ERROR);
        }*/
        this.save(commentReply);

        CommentReplyVO vo = BeanHelper.copyProperties(commentReply, CommentReplyVO.class);
        vo.setCommentId(Long.valueOf(commentReply.getCommentId()).intValue());
        return ResponseResult.okResult(vo);


    }
    @Autowired
    private CommentService commentService;
    // 删除评论
    @Override
    public ResponseResult deleteByCommentId(Integer commentId) {
        // 删除评论表
        LambdaQueryWrapper<Comment> qw = new LambdaQueryWrapper<>();
        qw.eq(Comment::getId,commentId);
        commentService.remove(qw);
        // 删除评论回复表
        LambdaQueryWrapper<CommentReply> qw1 = new LambdaQueryWrapper<>();
        qw1.eq(CommentReply::getCommentId,commentId);
        this.remove(qw1);

        return ResponseResult.okResult();
    }
}
